package com.ayi.dao.userdao;

import com.ayi.utils.GetConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @author Ayingzz
 * @create 2021/12/6 19:36
 */
public class UpdateUser {

    /**
     * 更换用户账号的 启用/禁用 状态
     * @param id
     * @return
     */
    public boolean changeStatusById(int id) {
        Connection conn = new GetConn().getConnection();
        boolean isStop = new QueryUser().queryUserById(id).isStop();
        String sql = "update Users set isStop = ? where id = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setBoolean(1, isStop == true ? false : true);
            ps.setInt(2, id);
            ps.execute();
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 更新某个管理员的信息
     * @param id
     * @param username
     * @param password
     * @return
     */
    public boolean updateAdminFromUsers(int id, String username, String password) {
        Connection conn = new GetConn().getConnection();
        String sql = "update Users set username = ?, password = ? where id = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ps.setInt(3, id);
            ps.execute();
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}
